package com.abocode.aichat.configuration;

import com.abocode.aichat.core.application.AuthHandler;
import com.abocode.aichat.core.application.ChatMessageHandler;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.context.endpoint.ProtocolEndpoint;
import com.abocode.aichat.server.modules.Module;
import com.abocode.aichat.server.modules.ModuleServiceHandler;
import com.abocode.aichat.server.modules.handler.*;
import com.abocode.aichat.server.modules.AuthModuleHandler;
import com.abocode.aichat.server.modules.ChatModuleHandler;
import com.abocode.aichat.server.modules.PresenceModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class AppConfiguration {
    @Autowired
    private ServerRuntimeContext serverRuntimeContext;
    @Autowired
    private AiChatProperties aiChatProperties;

    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public ProtocolEndpoint protocolEndpoint(){
        ProtocolEndpoint protocolEndpoint=new ProtocolEndpoint();
        protocolEndpoint.setName("protocol");
        protocolEndpoint.setModuleRegistries(modules());
        protocolEndpoint.setDomain(aiChatProperties.getDomain());
        protocolEndpoint.setPort(aiChatProperties.getPort());
        protocolEndpoint.setChildSize(100);
        protocolEndpoint.setParentSize(100);
        protocolEndpoint.setServerRuntimeContext(serverRuntimeContext);
        return protocolEndpoint;
    }
    private List<Module> modules(){
        List<Module> moduleRegistries =new ArrayList<>();
        moduleRegistries.add(authModule());
        moduleRegistries.add(chatModule());
        moduleRegistries.add(presenceModule());
        return moduleRegistries;
    }

    @Autowired
    public AuthHandler authHandler;
    private AuthModuleHandler authModule(){
        List<ModuleServiceHandler> handlers=new ArrayList<>();
        handlers.add(authHandler);
        AuthModuleHandler authModule=new AuthModuleHandler();
        authModule.registerModuleServices(handlers);
        return  authModule;
    }

    @Autowired
    private  ChatMessageHandler chatMessageHandler;
    private ChatModuleHandler chatModule(){
        List<ModuleServiceHandler> handlers=new ArrayList<>();
        handlers.add(new ChatConfirmServiceHandler());
        handlers.add(new ChatOfflineServiceHandler());
        handlers.add(chatMessageHandler);
        ChatModuleHandler chatModule=new ChatModuleHandler();
        chatModule.registerModuleServices(handlers);
        return  chatModule;
    }

    private PresenceModuleHandler presenceModule(){
        List<ModuleServiceHandler> handlers=new ArrayList<>();
        handlers.add(new PresenceGetServiceHandler());
        handlers.add(new PresenceOnlineServiceHandler());
        handlers.add(new PresenceSetServiceHandler());
        PresenceModuleHandler presenceModule=new PresenceModuleHandler();
        presenceModule.registerModuleServices(handlers);
        return  presenceModule;
    }
}
